package cn.wh.xwm.controller.action;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.hibernate.Transaction;

import net.sf.json.JSONObject;

import cn.wh.xwm.controller.action.vo.UserDisctrictView;
import cn.wh.xwm.hibernate.dao.CustomerinfoDAO;
import cn.wh.xwm.hibernate.dao.UserCustomerRelation;
import cn.wh.xwm.hibernate.dao.UserCustomerRelationDAO;
import cn.wh.xwm.hibernate.dao.UserCustomerRelationId;
import cn.wh.xwm.hibernate.dao.UserinfoDAO;

/**
 * 辖区管理
 * 
 * @author swimming
 * 
 */
public class UserCustomerRelationAction extends BaseAction {
	private String salesId;
	private String customerId;

	/**
	 * 根据SALESID获取区域客户
	 * 
	 * @throws IOException
	 */
	public void findAllDistrictBySales() throws IOException {
		UserCustomerRelationDAO dao = new UserCustomerRelationDAO();
		CustomerinfoDAO cdao = new CustomerinfoDAO();
		UserinfoDAO udao = new UserinfoDAO();
		List<UserCustomerRelation> list = dao.findByProperty("id.userId", salesId);
		if (list == null || list.size() == 0) {
			JSONObject object = new JSONObject();
			object.put("success", false);
			dao.getSession().close();
			this.responseJson(object);
		} else {
			JSONObject object = new JSONObject();
			List<UserDisctrictView> vlist =  new ArrayList<UserDisctrictView>();
			for (UserCustomerRelation info : list) {
				UserDisctrictView view = new UserDisctrictView();
				view.setCustomername(cdao.findById(info.getId().getCustomerId()).getName());
				view.setSalesname(udao.findById(info.getId().getUserId()).getUsername());
				view.setCustomerId(info.getId().getCustomerId());
				view.setSalesId(info.getId().getUserId());
				vlist.add(view);
			}
			object.put("rows", vlist);
			object.put("total", list.size());
			dao.getSession().close();
			this.responseJson(object);
		}
	}

	/**
	 * 删除辖区客户
	 * @throws IOException
	 */
	public void deleteRelation() throws IOException{
		UserCustomerRelationDAO dao = new UserCustomerRelationDAO();
		UserCustomerRelationId id = new UserCustomerRelationId();
		id.setCustomerId(customerId);
		id.setUserId(salesId);
		UserCustomerRelation info = dao.findById(id);
		Transaction ts = dao.getSession().getTransaction();
		ts.begin();
		dao.delete(info);
		ts.commit();
		JSONObject object = new JSONObject();
		object.put("success", true);
		dao.getSession().close();
		this.responseJson(object);
	}
	/**
	 * 通过ajax提交
	 * @throws IOException
	 */
	public void addRelation() throws IOException {
		UserCustomerRelationDAO dao = new UserCustomerRelationDAO();
		UserCustomerRelation info = new UserCustomerRelation();
		UserCustomerRelationId id = new UserCustomerRelationId();
		info.setId(id);
		id.setCustomerId(customerId);
		id.setUserId(salesId);
		Transaction ts = dao.getSession().getTransaction();
		ts.begin();
		dao.save(info);
		ts.commit();
		JSONObject object = new JSONObject();
		object.put("success", true);
		dao.getSession().close();
		this.responseJson(object);
	}

	public String getCustomerId() {
		return customerId;
	}

	public void setCustomerId(String customerId) {
		this.customerId = customerId;
	}

	public String getSalesId() {
		return salesId;
	}

	public void setSalesId(String salesId) {
		this.salesId = salesId;
	}


}
